package lk.icta.common.dao.impl;

import lk.icta.common.dao.ChildDAO;
import lk.icta.common.entity.Child;
import lk.icta.user.entity.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author Duminda Dharmakeerthi
 */
public class ChildDAOImpl extends BaseDAO implements ChildDAO {

    @Transactional(propagation = Propagation.MANDATORY)
    public void addChild(Child child, User user) {
        logger.debug("Adding a child");
        em.persist(child);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void updateChild(Child child, User user) {
        logger.debug("Updating a child");
        em.merge(child);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void deleteChild(Child child, User user) {
        logger.debug("Delete a child {} by {}", child.getFullName(), user.getUsername());
        em.remove(child);
        em.flush();
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public Child getChildById(long id) {
        logger.debug("Loading child {}", id);
        return em.find(Child.class, id);
    }
}
